<?php
namespace app\swoole\controller;

use \app\swoole\model\ScheduleTask;

class Task extends \think\Controller {

    private $__model_scheduletask;

    public function _initialize() {
        $this->__model_scheduletask = new ScheduleTask();
    }

    /**
     * 显示所有存在的任务
     * @return template
     */
    public function show() {
        $data = ScheduleTask::all();
        return $this->fetch('tasklist', ['data' => $data]);
    }

    /**
     * 更新任务状态的操作
     * @param type $id
     * @param type $status
     */
    public function ajaxUpdate($id, $status) {
        ScheduleTask::update(['status' => $status], ['id' => $id]);
        $this->result(['id' => $id, 'status' => $status], 1);
    }

    /**
     * 添加一个定时任务
     * @return type
     */
    public function create() {
        $data = [];
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = $this->request->param();
            $status = $this->validate($data, 'ScheduleTask');
            if (TRUE !== $status) {
                $this->result([], 0, $status);
            }
            ScheduleTask::create($data);
            $this->result($data, 1, '添加成功');
        } elseif ($this->request->isGet() && $this->request->param('id')) {
            $id = $this->request->param('id');
            $data = ScheduleTask::get($id)->getData();
        }
        return $this->fetch('add', ['data' => $data]);
    }

}
